package com.ssm.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ssm.entity.AccountEntity;
import com.ssm.entity.EnterpriseEntity;
import com.ssm.entity.StudentEntity;
import com.ssm.service.AccountService;
import com.ssm.service.EnterpriseService;
import com.ssm.service.StudentService;

import myUtils.Response;
import myUtils.Utils;

@RestController
public class UserController {
    @Autowired
    @Qualifier("AccountServiceImpl")
    private AccountService accountService;
    
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    
    @Autowired
    @Qualifier("EnterpriseServiceImpl")
    private EnterpriseService enterpriseService;
    
	@RequestMapping(value="/createAccount", method=RequestMethod.POST)
	public Response createAccount(
		@RequestParam("username") String username,
		@RequestParam("password") String password,
		@RequestParam("type") int type,
		@RequestParam(value="login_time") long loginTime,
		@RequestParam(value="avatar", required=false) String avatarName
	) {
		AccountEntity account = new AccountEntity();
		account.setUsername(username);
		account.setPassword(password);
		account.setType(type);
		if (avatarName != null) { account.setAvatar(avatarName); }
		//登录时间
		Date userLoginTime = new Date(loginTime);
		account.setLoginTime(userLoginTime);
		accountService.save(account);
		return Response.successWrap(account);
	}
	
	@RequestMapping(value="/uploadImage", method=RequestMethod.POST)
	public Response uploadAvatar(
		HttpServletRequest request,
		@RequestParam(value="image", required=true) MultipartFile image
	) {
		String imagePath = request.getSession().getServletContext().getRealPath("/upload/image");
		System.out.println(imagePath);
		String avatarName = Utils.uploadFileHandler(imagePath, image);
		
		if (avatarName != null) { return Response.successWrap(avatarName); }
		else {
			return Response.failWrap("上传图片失败");
		}
	}
	
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public Response login(
		@RequestParam("username") String username,
		@RequestParam("password") String password
	) {
		AccountEntity result = new AccountEntity();
		result = accountService.validateLogin(username, password);
		if (result != null) {
			int type = result.getType();
			String accountId = result.getAccountId();
			if (type == 1) {
				StudentEntity student= studentService.findByAccountId(accountId);
				return Response.successWrap(result, student);
			} else if (type == 2) {
				EnterpriseEntity company = enterpriseService.findByAccountId(accountId);
				return Response.successWrap(result, company);
			}
		}
		return Response.failWrap("登录失败");
	}
}
